package ui.component
{
	import assets.Asset;
	
	import events.LoadedEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import tool.BitmapTool;
	
	public class ConfirmBox extends Sprite
	{
		private var _bitmapURL:String = Global.assetPath+"UI/confirm.png";
		private var _bmdLoader:Loader;
		
		private var _yesButton:Button;
		private var _noButton:Button;
		
		private var _confirmFun:Function;
		private var _negateFun:Function;
		
		///////////////////////////////////////////////////
		public function get negateFun():Function
		{
			return _negateFun;
		}
		
		public function set negateFun(value:Function):void
		{
			_negateFun = value;
			_noButton.clickFunction = value;
		}
		
		public function get confirmFun():Function
		{
			return _confirmFun;
		}
		
		public function set confirmFun(value:Function):void
		{
			_confirmFun = value;
			_yesButton.clickFunction = value;
		}
		///////////////////////////////////////////////////
		/**
		 * 确认框
		 * 
		 * @param ConfirmFun 当点击 “是” 时，触发的方法
		 * @param NegateFun 当点击 “否” 时，触发的方法
		 */
		public function ConfirmBox(ConfirmFun:Function=null,NegateFun:Function=null)
		{
			_confirmFun = ConfirmFun;
			_negateFun = NegateFun;
			
			Asset.bitmapdata.addEventListener(LoadedEvent.BITMAPDATA_COMPLETE,bitmapDataLoaded);
			Asset.bitmapdata.load(_bitmapURL);
			
			addEventListener(Event.ADDED_TO_STAGE,onAdded);
			addEventListener(Event.REMOVED,onRemove);
		}
		
		private function bitmapDataLoaded(e:LoadedEvent):void
		{
			if(e.URL == _bitmapURL)
			{
				Asset.bitmapdata.removeEventListener(LoadedEvent.BITMAPDATA_COMPLETE,bitmapDataLoaded);
				var bmd:BitmapData = Asset.resBitmapDatas[_bitmapURL];
				
				var bmds:Array = BitmapTool.cutImageWithSameSize(bmd,1,1,0,0,211,83);
				var backgroundBmd:BitmapData = bmds[0];
				
				addChild(new Bitmap(backgroundBmd));
				
				bmds = BitmapTool.cutImageWithSameSize(bmd,2,3,0,83,86,23);
				
				_yesButton = new Button(bmds[0],bmds[2],bmds[4],_confirmFun);
				_noButton = new Button(bmds[1],bmds[3],bmds[5],_negateFun);
				
				_yesButton.x = 12;
				_noButton.x = 113
				_yesButton.y =_noButton.y = 45;
				
				addChild(_yesButton);
				addChild(_noButton);
				
				x = (Global.stage.stageWidth-width)*0.5;
				y = (Global.stage.stageHeight-height)*0.5;
			}
		}
		
		private function onResize(e:Event):void
		{
			x = (Global.stage.stageWidth-width)*0.5;
			y = (Global.stage.stageHeight-height)*0.6;
		}
		private function onAdded(e:Event):void
		{
			x = (Global.stage.stageWidth-width)*0.5;
			y = (Global.stage.stageHeight-height)*0.6;
			Global.stage.addEventListener(Event.RESIZE,onResize);
		}
		private function onRemove(e:Event):void
		{
			Global.stage.removeEventListener(Event.RESIZE,onResize);
		}
		
	}
}

